package com.jsmtr.rms.utils;

import com.jsmtr.rms.entity.DNZResume;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public class BeanTools {

    public static void main(String[] args) {
        System.out.println(UUID.randomUUID().toString());
    }

    /**
     * 将实体source中的同名属性复制到实体dest
     * @param source 原始数据
     * @param dest  目标数据
     * @return BaseExaminerGroup
     * @throws Exception
     */
    public static void Copy(Object source, Object dest) throws Exception {
        //获取属性
        BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), Object.class);
        PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();

        BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), Object.class);
        PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();

        try {
            for (int i = 0; i < sourceProperty.length; i++) {
                for (int j = 0; j < destProperty.length; j++) {
                    if (sourceProperty[i].getName().equals(destProperty[j].getName())) {

                        System.out.println(sourceProperty[i].getName());
                        destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source));
                        break;
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception("属性复制失败:" + e.getMessage());
        }
    }

    public static String hqlWhere(Object source, String shortname) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass(), Object.class);
            PropertyDescriptor[] property = beanInfo.getPropertyDescriptors();
            StringBuffer condition = new StringBuffer();
            for (int i = 0; i < property.length; i++) {
                Object val = property[i].getReadMethod().invoke(source);

                if (property[i].getPropertyType().equals(Integer.class)
                        && val != null && (Integer) val != -1) {
                    condition.append(" and " + shortname + "." + property[i].getName() + "=" + val);
                }
               // System.out.println(property[i].getDisplayName()+":"+property[i].getName());
                if (property[i].getName().toUpperCase().indexOf("STATE")>=0&& StringTools.trimNotEmpty(val)
                        && !val.toString().trim().equals("-1")){
                    condition.append(" and " + shortname + "." + property[i].getName() + "='" + val+"'");
                }else if (property[i].getName().toUpperCase().indexOf("RESULT")>=0&& StringTools.trimNotEmpty(val)
                        && !val.toString().trim().equals("-1")) {
                    condition.append(" and " + shortname + "." + property[i].getName() + "='" + val+"'");
                }else if (property[i].getName().toUpperCase().indexOf("DATE")>=0&& StringTools.trimNotEmpty(val)
                        && !val.toString().trim().equals("-1")) {
                    condition.append(" and to_char(" + shortname + "." + property[i].getName() + ",'yyyy-MM-dd' ) like '%" + val+"%'");
                }else if (property[i].getPropertyType().equals(String.class)
                        && StringTools.trimNotEmpty(val)
                        && !val.toString().trim().equals("-1")) {
                    condition.append(" and " + shortname + "." + property[i].getName()
                            + " like '%" + val.toString() + "%'");
                }


            }
            return condition.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    @SuppressWarnings("rawtypes")
    public static void checkListItem(List list) throws Exception {
        if (list == null || list.size() == 0) {
            throw new Exception("List isEmpty");
        }
        for (int i = list.size() - 1; i >= 0; i--) {
            if (!notNull(list.get(i))) {
                list.remove(i);
            }
        }
        if (list.size() == 0) {
            throw new Exception("List isEmpty");
        }
    }

    /*
     * 判断实体是否包含空值
     * 包含返回true  不包含返回false
     */
    @SuppressWarnings("rawtypes")
    public static boolean notNull(Object obj) {
        if (obj == null) {
            return false;
        }
        boolean notNull = false;
        boolean fieldAccess = false;
        Field[] fields = null;
        Field field = null;
        Class type = null;
        for (Class clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
            fields = clazz.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                field = fields[i];
                type = field.getType();
                //岗位模版字段中以remark开始的字段忽略
                if (!notNull && !field.getName().startsWith("remark") && !type.equals(Set.class)) {
                    fieldAccess = field.isAccessible();
                    if (!fieldAccess) {
                        field.setAccessible(true);
                    }
                    try {
                        if (field.get(obj) == null) {
                            notNull = true;
                            field.setAccessible(fieldAccess);
                            break;
                        }
                    } catch (Exception e) {
                    }
                    field.setAccessible(fieldAccess);
                }
            }
        }
        return notNull;
    }
}